package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.InciDao;
import com.adderis.soft.domain.model.Inci;

@Repository("inciDao")
public class InciDaoImpl implements InciDao {

	private static final Log log = LogFactory.getLog(InciDaoImpl.class);

	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp = new java.sql.Timestamp(now.getTime());;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(Inci transientInci) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientInci.setCreatetimestamp(nowtimesstamp);
			transientInci.setStatutcode("A");
			entityManager.persist(transientInci);

			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(Inci persistentInci) {
		log.debug("removing Inci instance");
		try {
			persistentInci.setStatutcode("D");
			persistentInci.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentInci);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(Integer InciId) {
		this.remove(this.findById(InciId));

	}

	/**
	 * {@inheritDoc}
	 */
	public Inci merge(Inci detachedInci) {
		log.debug("merging Inci instance");
		try {
			detachedInci.setUpdatetimesstamp(nowtimesstamp);
			Inci result = entityManager.merge(detachedInci);
			entityManager.merge(detachedInci);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Inci findById(Integer id) {
		log.debug("getting Inci instance with id: " + id);
		try {
			Inci instance = entityManager.find(Inci.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<Inci> findAll() {
		log.debug("getting all Inci instance");
		try {
			Query query = entityManager.createQuery("SELECT c FROM Inci c");
			List<Inci> inciList = (List<Inci>) query.getResultList();
			log.debug("findAll successful");
			return inciList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<Inci>();
		}

	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Inci> search(String searchString) {
		log.debug("Search Inci instances with search string: " + searchString);
		try {
			Query query = entityManager.createQuery("select nc from Inci nc where nc.INCI like :searchString");
			query.setParameter("searchString", searchString);
			List<Inci> inciList = (List<Inci>) query.getResultList();
			log.debug("search successful");
			return inciList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<Inci>();
		}
	}

}
